package com.bite.forum.services.impl;

import com.bite.forum.common.AppResult;
import com.bite.forum.common.ResultCode;
import com.bite.forum.dao.BoardMapper;
import com.bite.forum.exception.ApplicationException;
import com.bite.forum.model.Board;
import com.bite.forum.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        // 判断num是否小于等于0
        if(num <= 0){
            // 打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 调用boardMapper的selectByNum方法，根据num查询Board列表
        List<Board> result = boardMapper.selectByNum(num);
        // 返回查询结果
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        // 判断id是否为空或小于等于0
        if(id == null || id <= 0){
            // 记录警告日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 根据id查询Board对象
        Board board = boardMapper.selectByPrimaryKey(id);
        // 判断Board对象是否为空
        if(board == null){
            // 记录警告日志
            log.warn(ResultCode.ERROR_IS_NULL + "，board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.ERROR_IS_NULL));
        }

        // 创建新的Board对象
        Board newBoard = new Board();
        // 设置id
        newBoard.setId(board.getId());
        // 设置文章数量加1
        newBoard.setArticleCount(board.getArticleCount() + 1);
        // 更新Board对象
        boardMapper.updateByPrimaryKeySelective(newBoard);
    }

    @Override
    public Board selectById(Long id) {
        // 判断id是否为空或小于等于0
        if(id == null || id <= 0){
            // 记录警告日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 根据id查询Board对象
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleCountById(Long id) {
        // 判断id是否为空或小于等于0
        if(id == null || id <= 0){
            // 记录警告日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        // 根据id查询Board对象
        Board board = boardMapper.selectByPrimaryKey(id);
        // 判断Board对象是否为空
        if(board == null){
            // 记录警告日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS + "，board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board newBoard = new Board();
        newBoard.setId(board.getId());
        newBoard.setArticleCount(board.getArticleCount() - 1);
        if(newBoard.getArticleCount() < 0){
            newBoard.setArticleCount(0);
        }
        boardMapper.updateByPrimaryKeySelective(newBoard);
    }
}
